﻿using Newtonsoft.Json.Linq;
using ProjScan.Tools;
using System;
using System.Collections.Generic;

namespace ProjScan.Config
{
    //
    // 摘要:
    //     用来存储全局配置信息，如是否开启每一个模块的检测 Class3
    internal class GlobalConfig : BaseConfig
    {
        public enum e_Global_Assets_Scan_Mode
        {
            All,
            TargetOnly,
            TargetOnlyWithDepends
        }

        public Dictionary<string, bool> scanModules = null;

        public Dictionary<string, bool> fixModules = null;

        private bool isInit = false;

        private JArray targetConfigNode;

        private JArray ignoreConfigNode;

        private JObject runSelectedModulesNode;

        private JObject fixSelectedModulesNode;

        public string failedDataFolderPath = string.Empty;

        private string scanModeNode;

        public e_Global_Assets_Scan_Mode globalAssetsScanMode;

        public List<ScanAssetsConfig> globalTargetConfigs = null;

        public List<ScanAssetsConfig> globalIgnoreConfigs = null;

        public override void Init()
        {
            globalAssetsScanMode = e_Global_Assets_Scan_Mode.All;
            if (globalTargetConfigs == null)
            {
                globalTargetConfigs = new List<ScanAssetsConfig>();
            }
            else
            {
                globalTargetConfigs.Clear();
            }

            if (globalIgnoreConfigs == null)
            {
                globalIgnoreConfigs = new List<ScanAssetsConfig>();
            }
            else
            {
                globalIgnoreConfigs.Clear();
            }

            failedDataFolderPath = "";
            scanModules = new Dictionary<string, bool>
            {
                {
                    "Basic_Assets_Check",
                    true
                },
                {
                    "Scenes_Check",
                    true
                },
                {
                    "Global_Settings_Check",
                    true
                },
                {
                    "CS_Check",
                    true
                },
                {
                    "Lua_Check",
                    true
                },
                {
                    "Custom_Rules_Check",
                    true
                },
                {
                    "Effects_Play_Check",
                    true
                },
                {
                    "Shader_Analysis",
                    true
                },
                {
                    "Art_Assets_Check",
                    true
                }
            };
            fixModules = new Dictionary<string, bool>
            {
                {
                    "Basic_Assets_Fix",
                    true
                },
                {
                    "Scenes_Fix",
                    true
                },
                {
                    "Global_Settings_Fix",
                    true
                },
                {
                    "CS_Fix",
                    false
                },
                {
                    "Lua_Fix",
                    false
                },
                {
                    "Custom_Rules_Fix",
                    false
                },
                {
                    "Effects_Play_Fix",
                    false
                },
                {
                    "Shader_Fix",
                    false
                },
                {
                    "Art_Assets_Fix",
                    false
                }
            };
            isInit = true;
        }

        public override void LoadConfig(JObject data)
        {
            _rootConfig = (JObject)data["General"];
            scanModeNode = _rootConfig["Global_Assets_Scan_Mode"].Value<string>();
            failedDataFolderPath = _rootConfig["Global_Failed_Data_Folder"].Value<string>();
            targetConfigNode = (JArray)_rootConfig["global_target_config"];
            ignoreConfigNode = (JArray)_rootConfig["global_ignore_config"];
            runSelectedModulesNode = (JObject)_rootConfig["RunSelectedModules_Includes"];
            fixSelectedModulesNode = (JObject)_rootConfig["FixSelectedModules_Includes"];
            if (scanModeNode.IndexOf("TargetOnly", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                globalAssetsScanMode = e_Global_Assets_Scan_Mode.TargetOnly;
                if (scanModeNode.IndexOf("TargetOnlyWithDepends", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    globalAssetsScanMode = e_Global_Assets_Scan_Mode.TargetOnlyWithDepends;
                }
            }
            else
            {
                globalAssetsScanMode = e_Global_Assets_Scan_Mode.All;
            }

            foreach (JObject item3 in targetConfigNode)
            {
                ScanAssetsConfig item = JObjectToScanAssetsConfig(item3);
                globalTargetConfigs.Add(item);
            }

            foreach (JObject item4 in ignoreConfigNode)
            {
                ScanAssetsConfig item2 = JObjectToScanAssetsConfig(item4);
                globalIgnoreConfigs.Add(item2);
            }

            foreach (KeyValuePair<string, JToken> item5 in runSelectedModulesNode)
            {
                if (scanModules.ContainsKey(item5.Key))
                {
                    scanModules[item5.Key] = ((item5.Value.Value<bool>() ? ((byte)1) : ((byte)0)) != 0);
                }
            }

            foreach (KeyValuePair<string, JToken> item6 in fixSelectedModulesNode)
            {
                if (fixModules.ContainsKey(item6.Key))
                {
                    fixModules[item6.Key] = ((item6.Value.Value<bool>() ? ((byte)1) : ((byte)0)) != 0);
                }
            }

            globalIgnoreConfigs = MergeScanAssetsConfigLists(globalIgnoreConfigs, Singleton<QAOnlineSettings>.Instance.IgnoreDirList);
        }

        private List<ScanAssetsConfig> MergeScanAssetsConfigLists(List<ScanAssetsConfig> list1, List<ScanAssetsConfig> list2)
        {
            Dictionary<(string, MatchingType), ScanAssetsConfig> dictionary = new Dictionary<(string, MatchingType), ScanAssetsConfig>();
            if (list1 != null)
            {
                foreach (ScanAssetsConfig item in list1)
                {
                    if (!string.IsNullOrWhiteSpace(item.assetPath))
                    {
                        (string, MatchingType) key = (item.assetPath, item.matchingType);
                        dictionary[key] = item;
                    }
                }
            }

            if (list2 != null)
            {
                foreach (ScanAssetsConfig item2 in list2)
                {
                    if (!string.IsNullOrWhiteSpace(item2.assetPath))
                    {
                        (string, MatchingType) key2 = (item2.assetPath, item2.matchingType);
                        if (!dictionary.ContainsKey(key2))
                        {
                            dictionary[key2] = item2;
                        }
                    }
                }
            }

            return new List<ScanAssetsConfig>(dictionary.Values);
        }

        public override void SaveConfig(JObject data)
        {
            JObject jObject = new JObject();
            foreach (KeyValuePair<string, bool> scanModule in scanModules)
            {
                jObject.Add(new JProperty(scanModule.Key, scanModule.Value));
            }

            JObject jObject2 = new JObject();
            foreach (KeyValuePair<string, bool> fixModule in fixModules)
            {
                jObject2.Add(new JProperty(fixModule.Key, fixModule.Value));
            }

            JArray jArray = new JArray();
            foreach (ScanAssetsConfig globalTargetConfig in globalTargetConfigs)
            {
                JObject item = new JObject
                {
                    {
                        "matching_str",
                        (JToken)globalTargetConfig.assetPath
                    },
                    {
                        "matching_type",
                        (JToken)(int)globalTargetConfig.matchingType
                    }
                };
                jArray.Add(item);
            }

            JArray jArray2 = new JArray();
            foreach (ScanAssetsConfig globalIgnoreConfig in globalIgnoreConfigs)
            {
                JObject item2 = new JObject
                {
                    {
                        "matching_str",
                        (JToken)globalIgnoreConfig.assetPath
                    },
                    {
                        "matching_type",
                        (JToken)(int)globalIgnoreConfig.matchingType
                    }
                };
                jArray2.Add(item2);
            }

            string value = "";
            switch (globalAssetsScanMode)
            {
                case e_Global_Assets_Scan_Mode.TargetOnly:
                    value = "TargetOnly";
                    break;
                case e_Global_Assets_Scan_Mode.TargetOnlyWithDepends:
                    value = "TargetOnlyWithDepends";
                    break;
                case e_Global_Assets_Scan_Mode.All:
                    value = "All";
                    break;
            }

            _rootConfig["Global_Assets_Scan_Mode"] = (JToken)value;
            _rootConfig["global_target_config"] = jArray;
            _rootConfig["global_ignore_config"] = jArray2;
            _rootConfig["RunSelectedModules_Includes"] = jObject;
            _rootConfig["FixSelectedModules_Includes"] = jObject2;
            data.Add("General", _rootConfig);
        }
    }
}